package com.riekoff.particlecubeworld.player;

import cc.creativecomputing.graphics.CCColor;
import cc.creativecomputing.io.CCIOUtil;
import cc.creativecomputing.math.CCMath;
import cc.creativecomputing.util.CCFormatUtil;

public class APattern {
	public int height = 0;
	public int width = 0;

	public boolean[][] verticals;
	public boolean[][] horizontals;

	public int[][] tileindices;
	
	int[][] tileX;
	int[][] tileY;
	int[][] tileOffset;

	CCColor[] colors = null;

	public APattern(int theWidth, int theHeight) {
		reset(theWidth, theHeight);
	}
	
	public APattern(String theFile) {
		loadPattern(theFile);
	}

	private void reset(int theWidth, int theHeight) {
		width = theWidth;
		height = theHeight;
		verticals = new boolean[theWidth][theHeight];
		horizontals = new boolean[theWidth][theHeight];
		tileindices = new int[theWidth][theHeight];

		tileX = new int[theWidth][theHeight];
		tileY = new int[theWidth][theHeight];
		tileOffset = new int[theWidth][theHeight];
	}

	long myLast = 0;
	
	public void loadPattern(String theFile){
		String[] myPatternLines = CCIOUtil.loadStrings(theFile.replaceAll("\\\\", "/"));
		loadPattern(myPatternLines);
	}
	
	public void loadPattern(String[] myPatternLines){
		int myScale = 1;
		boolean myHasScale = false;
		if(myPatternLines.length > 0){
			try{
				myScale = Integer.parseInt(myPatternLines[0]);
				myHasScale = true;
			}catch(Exception e){
				
			}
		}
		int myHeight = myPatternLines.length - (myHasScale ? 1 : 0);
		int myWidth = 0;
		for (int i = myHasScale ? 1 : 0; i < myPatternLines.length;i++) {
			String myLine = myPatternLines[i];
			myWidth = CCMath.max(myWidth, myLine.length() / 2);
		}
		if (myWidth == 0 || myHeight == 0) {
			return;
		}
		reset(myWidth * myScale, myHeight * myScale);
		
		boolean[][] myVerticals = new boolean[myWidth][myHeight];
		boolean[][] myHorizontals = new boolean[myWidth][myHeight];
		
		int y = 0;
		for (int i = myHasScale ? 1 : 0; i < myPatternLines.length;i++) {
			String myLine = myPatternLines[i];
			for (int x = 0; x < myWidth; x++) {
				myHorizontals[x][y] = myLine.length() > (x * 2 + 1) && myLine.charAt(x * 2) == '_';
				myVerticals[x][y] = myLine.length() > (x * 2 + 1) && myLine.charAt(x * 2 + 1) == '|';
			}

			y++;
		}
		
		for( y = 0; y < myHeight;y++){
			for (int x = 0; x < myWidth; x++) {
//				for(int s = 0; s < myScale; s++){
//					horizontals[x * myScale + s][y * myScale + (myScale - 1)] = myHorizontals[x][y];
//					verticals[x * myScale + (myScale - 1)][y * myScale + s] = myVerticals[x][y];
//				}
				horizontals[x][y] = myHorizontals[x][y];
				verticals[x][y] = myVerticals[x][y];
			}
		}
		findCells();
//		printCells();
	}

	void findCells() {
		int index = 1;
		for (int y = 0; y < height; y++) {
			int myTileX = 0;
			for (int x = 0; x < width; x++) {
				if (x > 0 && verticals[x - 1][y]){
					myTileX = x;
				}
				if (tileindices[x][y] > 0){
//					
					continue;
				}
				if (y == 0) {
					for (int y1 = height - 1; y1 >= 0; y1--) {
						if (horizontals[x][y1])
							break;
						if (x == 0) {
							for (int x1 = width - 1; x1 >= 0; x1--) {
								if (verticals[x1][y1])
									break;

								tileindices[x1][y1] = index;
								tileX[x1][y1] = 0;
								tileY[x1][y1] = 0;
								tileOffset[x1][y1] = 1;
							}
						}
						tileindices[x][y1] = index;
						tileOffset[x][y1] = 2;
					}
				}

				for (int y1 = y; y1 < height; y1++) {
					if (x == 0) {
						for (int x1 = width - 1; x1 >= 0; x1--) {
							if (verticals[x1][y1])
								break;

							tileindices[x1][y1] = index;
							tileX[x1][y1] = 0;
							tileY[x1][y1] = y;
							tileOffset[x1][y1] = 1;
						}
					}
					tileindices[x][y1] = index;
					tileX[x][y1] = myTileX;
					tileY[x][y1] = y;
					if (horizontals[x][y1])
						break;
				}
				// * height;

				if (verticals[x][y]){
					index++;
//					myTileX = x + 1;
				}

			}
		}
		colors = new CCColor[index];
		for (int i = 0; i < index; i++) {
			colors[i] = CCColor.createFromHSB(i / (float) (index), 1f, 1f);
		}
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				tileindices[x][y]--;
			}
		}
	}

	void printCells() {
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				System.out.print(CCFormatUtil.nf(tileindices[x][y], 2) + " | ");
			}
			System.out.println();
		}
		System.out.println();
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				System.out.print(CCFormatUtil.nf(tileOffset[x][y], 2) + " | ");
			}
			System.out.println();
		}
	}

}